
// 封装 axios 

import axios from "axios"
import {Toast,Notify} from "vant"

const baseURL = "http://localhost:my Documents/"

export function showLoading(msg){
    msg = msg || '请求中'
    Toast.clear()
    Toast.loading({
        message: msg,
        forbidClick: true,
    });
}

export function showSuccess(msg){
    msg = msg || 'success'
    Toast.clear()
    Notify.clear()
    Notify({ type: 'success', message: msg });
}

export function showFail(msg){
    msg = msg || '请求失败'
    Toast.clear()
    Notify.clear()
    Notify({
        message: msg,
        color: '#ad0000',
        background: '#ffe1e1',
    }); 
}


// axios.defaults.baseURL = baseURL ;   // 反向代理 冲突 
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';   // 管理系统 
// axios.defaults.timeout =  60000;  // 60S 超时 



// 响应结束之后的拦截器  Add a response interceptor
axios.interceptors.response.use(function (response) {
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    console.log(response)
    Toast.clear()
    return response;
}, function (error) {
    showFail("响应失败")
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error);
});



// POST  GET  
export const get = (url,params,headers)=>{
    return new Promise(function(resolve,reject){
        axios.get(url,{
            params,   // 参数 
            headers,  // 请求头 
        })
        .then(res=>{
            resolve(res)
        })
        .catch(err=>{
            reject(err)
        })
    })
}

export const post = (url,body,headers)=>{
    return new Promise(function(resolve,reject){
        axios.post(url,body,{
            headers,  // 请求头 
        })
        .then(res=>{
            resolve(res)
        })
        .catch(err=>{
            reject(err)
        })
    })
}

export {baseURL , axios }